package com.zhaoxi.project.wms.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import com.zhaoxi.common.utils.DateUtils;
import com.zhaoxi.common.utils.SecurityUtils;
import com.zhaoxi.framework.web.PageData;
import com.zhaoxi.project.wms.mapper.ProcessMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.zhaoxi.project.wms.mapper.BomMapper;

/**
 * 生产物品Service业务层处理
 * 
 * @author zhaoxi
 * @date 2023-08-28
 */
@Service
public class BomServiceImpl
{
    @Autowired
    private BomMapper bomMapper;
    @Autowired
    private ProcessMapper processMapper;

    /**
     * 查询生产物品列表
     *
     * @return 生产物品
     */
    public List<PageData> getList(PageData pd)
    {
        return bomMapper.getList(pd);
    }

    /**
     * 查询生产物品
     *
     * @param productId 生产物品主键
     * @return 生产物品
     */
    public PageData getByProductId(Long productId)
    {
        return bomMapper.getByProductId(productId);
    }

    //查询生产物品行数
    public Long getCount(PageData pd) {
        return bomMapper.getCount(pd);
    }

    /**
     * 新增生产物品
     * @return 结果
     */
    @Transactional
    public int add(PageData pd)
    {
        pd.put("creatBy", SecurityUtils.getLoginUser().getUsername());
        pd.put("creatTime",DateUtils.getNowDate());
        return bomMapper.add(pd);
    }

    /**
     * 修改生产物品
     * @return 结果
     */
    @Transactional
    public int edit(PageData pd)
    {
        pd.put("updateTime",DateUtils.getNowDate());
        pd.put("updateBy", SecurityUtils.getLoginUser().getUsername());

        Long productId = pd.getLong("productId");
        List<PageData> pdLines = bomMapper.getBomLineByProductId(productId);
        if(pdLines!=null&&pdLines.size()>0){
            Long[] subProductIds = new Long[pdLines.size()];
            int i = 0;
            for (PageData pdLine : pdLines){
                subProductIds[i++] = pdLine.getLong("subProductId");
            }
            this.delLineBySubProductIds(subProductIds);
        }

        return bomMapper.edit(pd);
    }

    /**
     * 批量删除生产物品
     * @return 结果
     */
    @Transactional
    public int delByProductIds(Long[] productIds)
    {
        PageData pdLine = new PageData();
        HashSet<Long> set = new HashSet<>();
        for (Long productId : productIds){
            pdLine.put("productId",productId);
            List<PageData> pdLines = this.getLineList(pdLine);
            for (PageData item : pdLines){
                set.add(item.getLong("subProductId"));
            }
        }
        if(set.size() > 0){
            Long[] _ids = new Long[set.size()];
            int i = 0;
            for(Long id: set){
                _ids[i++] = id;
            }
            bomMapper.delLineBySubProductIds(_ids);
        }

        processMapper.delBPByProductId(productIds);
        return bomMapper.delByProductIds(productIds);
    }


/**       ----子数据----      */
    /**
     * 查询物料清单明细列表
     * @return
     */
    public List<PageData> getLineList(PageData pd)
    {
        List<PageData> pdLineList = bomMapper.getLineList(pd);
        if(pdLineList !=null){
            List<PageData> childList = this.getBomLineChildren(new ArrayList<>(), pdLineList);
            pdLineList.addAll(childList);
        }
        return pdLineList;
    }

    /**
     * 新增物料清单明细
     * @return
     */
    public int addLine(PageData pd)
    {
        pd.put("createTime",DateUtils.getNowDate());
        pd.put("createBy", SecurityUtils.getLoginUser().getUsername());
        return bomMapper.addLine(pd);
    }

    /**
     * 修改物料清单明细
     * @return
     */
    @Transactional
    public int editLine(PageData pd)
    {
        pd.put("updateTime",DateUtils.getNowDate());
        pd.put("updateBy", SecurityUtils.getLoginUser().getUsername());
        Long[] subProductId = new Long[1];
        subProductId[0] = pd.getLong("oldSubProductId");
        this.delLineBySubProductIds(subProductId);
        return bomMapper.addLine(pd);
    }

    /**
     * 删除物料清单明细
     * @return
     */
    public int delLineBySubProductIds(Long[] subProductIds) {
        PageData pdLine = new PageData();
        HashSet<Long> set = new HashSet<>();
        for (Long subProductId : subProductIds){
            pdLine.put("subProductId",subProductId);
            List<PageData> pdList = this.getLineList(pdLine);
            for (PageData item : pdList){
                set.add(item.getLong("subProductId"));
            }
        }
        Long[] _ids = new Long[set.size()];
        int i = 0;
        for(Long code: set){
            _ids[i++] = code;
        }
        return bomMapper.delLineBySubProductIds(_ids);
    }

    /**
     * 查询bomLine子节点
     */
    private List<PageData> getBomLineChildren(ArrayList<PageData> returnList, List<PageData> pdLines) {
        PageData childPdLine = new PageData();
        for(PageData pd : pdLines){
            childPdLine.put("productId",pd.getLong("subProductId"));
            List<PageData> childPdList = bomMapper.getLineList(childPdLine);
            if (childPdList!=null && childPdList.size() > 0){
                returnList.addAll(childPdList);
                this.getBomLineChildren(returnList,childPdList);
            }
        }
        return returnList;
    }


}
